﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Base.cshtml";
}
@section head{
    <script src="/content/scripts/modernizr-2.6.2.min.js" type="text/javascript"></script>
    <script src="/Content/scripts/jquery-validate/messages_cn.js"></script>
}
<input type="hidden" id="selpolicyid" />
<input type="hidden" id="edittype" />
<table border="0" width="100%" >
    <tr style="margin:5px">
        <td>
            <span style="font-size: 14px">关键字:</span>
            <input id="txtKey" name="txtKey"  type="text" class="input-medium search-query" placeholder="策略名称" style="width: 250px;
                background-color: #fff;"/>
            <button class="btn btn-primary btn-small" id="btnSearch">
                查询
            </button>
        </td>
        <td align="right">
            <button id="btnAdd" class="btn btn-primary btn-small" style="margin-right: 20px">
                新增策略
            </button>
        </td>
    </tr>
</table>
<table id="policygrid" class="mmg">
    <tr>
        <th rowspan="" colspan="">
        </th>
    </tr>
</table>
<div id="pg" style="text-align: right;">
</div>
<div id="policyform" class="modal fade" tabindex="-1" style="display: none;width:450px">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
            ×</button>
        <h4 class="modal-title" id="modaltitle">
            Responsive</h4>
    </div>
    <center>
    <div class="modal-body">
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td valign="middle" width="80px" align="right">
                    <span style="color:Red">*</span>策略名称:&nbsp;
                </td>
                <td>
                    <input type="text" id="txtPolicyName" placeholder="策略名称" class="col-xs-10 col-sm-5" />
                </td>
            </tr>
            <tr>
                <td valign="middle" align="right">
                    <span style="color:Red">*</span>结算周期:&nbsp;
                </td>
                <td>
                    <input type="text" id="txtDuration" placeholder="结算周期" class="col-xs-10 col-sm-5" />&nbsp;&nbsp;<span>月</span>
                </td>
            </tr>
            <tr>
                <td valign="middle" align="right">
                    <span style="color:Red">*</span>计算方式:&nbsp;
                </td>
                <td>
                    <select id="selSettlementType" class="selectpicker" data-style="btn-success">
                        <option value="0">按百分比</option>
                        <option value="1">固定值</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td><div style="height:10px; "></div></td>
            </tr>
            <tr>
                <td valign="middle" align="right">
                    <span style="color:Red">*</span>结算方式:&nbsp;
                </td>
                <td>
                    <select id="selBalanceType" class="selectpicker" data-style="btn-success">
                        <option value='0'>每期等额本金利息</option>;
                        <option value='1'>每期结息到期还本</option>;
                    </select>
                </td>
            </tr>
            <tr>
                <td><div style="height:10px; "></div></td>
            </tr>
            <tr>
                <td valign="middle" align="right">
                    <span style="color:Red">*</span>年化收益:&nbsp;
                </td>
                <td>
                    <input type="text" id="txtRate" placeholder="年化收益" class="col-xs-10 col-sm-5" />&nbsp;&nbsp;<span id="danwei">%</span>
                </td>
            </tr>
            <tr>
                <td valign="middle" align="right">
                    &nbsp;
                </td>
                <td>
                    <span id="lblmsg" style="color:Red">&nbsp;</span>
                </td>
            </tr>
        </table>
    </div>
    <div class="modal-footer">
        <button type="button" id="btnClose" data-dismiss="modal" class="btn btn-default">
            关闭</button>
        <button type="button" id="btnSave" class="btn btn-primary">
            保存</button>
    </div>
    </center>
</div>

@section tail{
    <script src="/content/plugins/mmgrid/mmGrid.js" type="text/javascript"></script>
    <script src="/content/plugins/mmgrid/mmPaginator.js" type="text/javascript"></script>
    @*<script src="/content/plugins/bootbox.min.js"></script>*@
    <script type="text/javascript">
        var cols = [
        { title: '策略名称', name: 'PolicyName', width: 120, align: 'center' },
        { title: '结算间隔', name: 'Duration', width: 30, align: 'center', renderer: function (val, item, index) {
            return val + "月";
        }
        },
        { title: '计算方式', name: 'SettlementType', width: 30, align: 'center', renderer: function (val, item, index) {
            if (val == 0) return "按比例";
            else if (val == 1) return "固定值";
        }
        },
        { title: '年化收益', name: 'Rate', width: 30, align: 'center', renderer: function (val, item, index) {
            if (item['SettlementType'] == 0) return val + "%";
            else if (item['SettlementType'] == 1) return val + "元";
        }
        },
        { title: '结算方式', name: 'BalanceType', width: 80, align: 'center', renderer: function (val, item, index) {
            if (item['BalanceType'] == 0) return '每期等额本金利息';
            else if (item['BalanceType'] == 1) return '每期结息到期还本';
            else if (item['BalanceType'] == 2) return '到期结本息';
            else if (item['BalanceType'] == 3) return '每期固定收益';
            else if (item['BalanceType'] == 4) return '到期固定收益';
        }
        },
        { title: '特殊策略', name: 'IsAttach', width: 30, align: 'center', renderer: function (val, item, index) {
            if (val == false) return '否';
            return '是';
        }
        },
        { title: '状态', name: 'Status', width: 30, align: 'center', renderer: function (val, item, index) {
            if (val == 0) return '禁用';
            return '正常';
        }
        },
        { title: '创建人', name: 'CreateUser', width: 40, align: 'center' },
        { title: '创建时间', name: 'CreateTime', width: 120, align: 'center' },
        { title: '操作', name: '', width: 170, align: 'center', lockWidth: true, lockDisplay: true, renderer: function (val, item, index) {
            var html = '<button  class="btn btn-info btn-mini" onclick="policyShow(\'' + item['ID'] + '\',\'' + item['PolicyName'] + '\',\'' + item['Duration'] + '\',\'' + item['SettlementType'] + '\',\'' + item['BalanceType'] + '\', \'' + item['Rate'] + '\')">编辑</button>';
            if (item['Status'] == 1) {
                html += ' <button  class="btn btn-info btn-mini" onclick="changeStatus(\'' + item['ID'] + '\', 0)">禁用</button>';
            }
            else {
                html += ' <button  class="btn btn-info btn-mini" onclick="changeStatus(\'' + item['ID'] + '\', 1)">启用</button>';
            }
            html += ' <button  class="btn btn-danger btn-mini" onclick="changeStatus(\'' + item['ID'] + '\', -1)">删除</button>';
            return html;
        }
        }
    ];

        var mmg;
        $(function () {
            $("#selSettlementType").change(function () {
                var options = "";
                $("#selBalanceType").html("");
                $("#danwei").html("");
                if (this.value == 0) {
                    options = "<option value='0'>每期等额本金利息</option>";
                    options += "<option value='1'>每期结息到期还本</option>";
                    $("#danwei").html("%");
                }
                else if (this.value == 1) {
                    options += "<option value='2'>到期结本息</option>";
                    options += "<option value='3'>每期固定收益</option>";
                    options += "<option value='4'>到期固定收益</option>";
                    $("#danwei").html("元");
                }
                $("#selBalanceType").html(options);
            });

            $("#btnAdd").click(function () {
                var policydiv = $("#policyform");
                var modaltitle = $("#modaltitle");
                modaltitle.text("新增策略");
                policydiv.modal();
                $("#edittype").val("1");
                $("#txtPolicyName").val("");
                $("#txtDuration").val("");
                $("#selSettlementType").val(0);
                $("#selBalanceType").val(0);
                $("#txtRate").val("");
                $("#selpolicyid").val("0");
                $("#lblmsg").html("");
            });

            $("#btnSave").click(function () {
                if (validate()) {
                    $.ajax({
                        url: "/Policy/PolicySave",
                        data: "editType=" + $("#edittype").val() + "&policyId=" + $("#selpolicyid").val() + "&policyName=" + $("#txtPolicyName").val() + "&balanceType=" + $("#selBalanceType").val()
                            + "&rate=" + $("#txtRate").val() + "&settlementType=" + $("#selSettlementType").val() + "&duration=" + $("#txtDuration").val(),
                        type: "post",

                        success: function (result) {
                            if (result == "True") {
                                $("#policyform").modal("hide");
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class=' icon-ok-sign bigger-290'></i>保存成功</span>", '300px', function () {
                                    search();
                                });
                            }
                            else {
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-remove-sign bigger-290'></i>保存失败</span>", '300px');
                            }
                        }
                    });
                }
            });

            mmg = $('#policygrid').mmGrid({
                indexCol: true,
                indexColWidth: 25,
                height: $("body", parent.document).height() - 220,
                cols: cols,
                url: '/Policy/GetPolicyListByPage',
                params: { key: $("#txtKey").val() },
                //method: 'get',
                remoteSort: true,
                //items: items,
                //sortName: 'SECUCODE',
                //sortStatus: 'asc',
                multiSelect: false,
                checkCol: false,
                fullWidthRows: true,
                autoLoad: false,
                plugins: [
                        $('#pg').mmPaginator()
                    ]
            });
            mmg.load();
            $("#btnSearch").click(function () {
                search();
            });
        });

        function validate() {
            $("#lblmsg").html("");
            var prodname = $("#txtPolicyName").val();
            if (prodname == "") {
                $("#txtPolicyName").focus();
                $("#lblmsg").html('政策名称不能为空');
                return false;
            }
            var prodnamereg = /^[A-Za-z0-9\u4e00-\u9fa5]+$/;
            if (!prodnamereg.test($("#txtPolicyName").val())) {
                $("#txtPolicyName").focus();
                $("#lblmsg").html('政策名称不能包含特殊符号');
                return false;
            }
            var durareg = /^[0-9]*[1-9][0-9]*$/;
            if (!durareg.test($("#txtDuration").val())) {
                $("#txtDuration").focus();
                $("#lblmsg").html('结算周期必须为正整数');
                return false;
            }
            var ratereg = /^(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d{1,2})?$/;   ///^\d+\.?\d*$/;
            if (!ratereg.test($("#txtRate").val()) || parseFloat($("#txtRate").val()) <= 0) {
                $("#txtRate").focus();
                $("#lblmsg").html('年化收益必须为浮点数字');
                return false;
            }
            return true;
        }

        function search() {
            mmg.setParm({ key: $("#txtKey").val() });
            mmg.load({ page: 1 });
        }

        function policyShow(policyid, policyname, duration, settype, baltype, rate) {
            var policydiv = $("#policyform");
            var modaltitle = $("#modaltitle");
            modaltitle.text("编辑政策");
            $("#txtPolicyName").val(policyname);
            $("#txtDuration").val(duration);
            $("#selSettlementType").val(settype);
            $("#selBalanceType").val(baltype);
            $("#txtRate").val(rate);
            $("#selpolicyid").val(policyid);
            $("#edittype").val("2");
            $("#lblmsg").html("");
            policydiv.modal();
        }

        // 改变产品状态
        function changeStatus(policyid, destinatstatus) {
            var deststatus;
            if (destinatstatus == 1) {
                deststatus = "启用";
            }
            else if (destinatstatus == 0) {
                deststatus = "禁用";
            }
            else if (destinatstatus == -1) {
                deststatus = "删除";
            }

            bootbox.confirm("<span style='font-size:14px;'><i style='color:green;' class='icon-question-sign bigger-290'></i> &nbsp;&nbsp;确定要" + deststatus + "该记录吗？</span>", '300px', function (confirmed) {
                if (confirmed == true) {
                    $.ajax({
                        url: '/Policy/PolicyStatusChange',
                        data: "policyId=" + policyid + "&destinationStatus=" + destinatstatus,
                        type: "post",
                        cache: false,
                        success: function (result) {
                            if (result == "True") {
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-ok-sign bigger-290'></i>   " + deststatus + "成功</span>", '300px');
                                mmg.load();
                            } else {
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-remove-sign bigger-290'></i>   " + deststatus + "失败</span>", '300px');

                            }
                        }
                    });
                }
            });
        }

    </script>
}
